<?php

/**
 *      [品牌空间] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: discuz.inc.php 4990 2010-11-23 03:22:20Z xuhui $
 */

if(!defined('IN_ADMIN')) {
	exit('Acess Denied');
}

// 处理提交过来的增加和修改操作
if(submitcheck('valuesubmit')){
	$conf = file_get_contents(B_ROOT.'config.php');
	$arr_data = array();
	$arr_data = $_POST['discuz'];
	
	$db_info = array('host' => $arr_data['dbhost'], 'name' => $arr_data['dbname'], 'pre' => $arr_data['dbpre'], 'user' => $arr_data['dbuser'], 'pw' => (strpos($arr_data['dbpw'], '******') === false) ? $arr_data['dbpw'] : $_SC['bbs_dbpw']);

	if ($db_info['host'] && $db_info['name'] && $db_info['user'] && $db_info['pw']) {
		$test_result = test_db($db_info);
		if ($test_result == -1) {
			$checkresults[] = array('discuz[dbhost]'=>$lang['discuz_db_error']);
			$checkresults[] = array('discuz[dbuser]'=>$lang['discuz_db_error']);
			strpos($arr_data['dbpw'], '******') && $checkresults[] = array('discuz[dbpw]'=>$lang['discuz_db_error']);
		} elseif ($test_result == 0) {
			$checkresults[] = array('discuz[dbname]'=>$lang['discuz_no_db']);
		} elseif ($test_result == -2) {
			$checkresults[] = array('discuz[dbpre]'=>$lang['discuz_db_pre_error']);
		} elseif ($test_result == 2 && $arr_data[version] == "discuzx") {
			$checkresults[] = array('discuz[version]'=>$lang['discuz_ver_7']);
		} elseif ($test_result == 3 && $arr_data[version] == "discuz") {
			$checkresults[] = array('discuz[version]'=>$lang['discuz_ver_x']);
		}
	}

	foreach($arr_data as $k=>$v){
		if(preg_match('/^db(.+?)$/',$k,$parem)){
			// 如果是密码，判断是不是含有＊，如果有六个，不修改
			if($k == 'dbpw') {
				if (strpos($v, '******') === false) {
					$conf = preg_replace('/\$_SC\[\'bbs_dbpw\'\].+;/','$_SC[\'bbs_dbpw\'] = \''.$v.'\';',$conf);
				}
				continue;
			}
			$conf = preg_replace('/\$_SC\[\'bbs_db'.$parem[1].'\'\].+;/','$_SC[\'bbs_db'.$parem[1].'\'] = \''.$v.'\';',$conf);
			continue;
		}
		if($k == 'url') {
			if (!preg_match('/^http:\/\//i', $v)) {
				$checkresults[] = array('discuz[url]'=>$lang['discuz_url_start_with_http']);
			}
			if (preg_match('/\.php$/i', $v)) {
				$checkresults[] = array('discuz[url]'=>$lang['discuz_url_cannot_end_with_forumphp']);
			}
			$conf = preg_replace('/\$_SC\[\'bbs_url\'\].+;/','$_SC[\'bbs_url\'] = \''.$v.'\';',$conf);
		}
		if($k == 'version') {
			$conf = preg_replace('/\$_SC\[\'bbs_version\'\].+;/','$_SC[\'bbs_version\'] = \''.$v.'\';',$conf);
		}
		/*
		if($k == 'key') {
			$conf = preg_replace('/\$_SC\[\'bbs_key\'\].+;/','$_SC[\'bbs_key\'] = \''.$v.'\';',$conf);
		}
		*/
	}

	if ($checkresults) {
		cpmsg('discuz_config_error', '', '', '', true, true, $checkresults);
		exit;
	}

	if(@fopen(B_ROOT.'config.php', 'w')) {
		file_put_contents(B_ROOT.'config.php', $conf);
		cpmsg('update_success', '?action=discuz');
	}else{
		cpmsg('Error : Config.php is not writeable . please repair it .');
	}
}

function test_db ($db) {
	if ($link = mysql_connect($db['host'], $db['user'], $db['pw'], 1)) {
		if (mysql_select_db($db['name'], $link)){
			if (mysql_query("select * from " . $db['pre'] . "forum_thread limit 1", $link)) {
				return 3;
			} else {
				if (mysql_query("select * from " . $db['pre'] . "threads limit 1", $link)) {
					return 2;
				} else {
					return -2;
				}
			}
			//pre_forum_post
			return 1;
		} else {
			return 0;
		}
	} else {
		return -1;
	}
}

// 数据库密码显示为＊
if(strlen($_SC['bbs_dbpw']) > 3) {
	$_SC['bbs_dbpw'] = substr($_SC['bbs_dbpw'], 0, 1) . '******' . substr($_SC['bbs_dbpw'], strlen($_SC['bbs_dbpw']) - 1);
} else {
	$_SC['bbs_dbpw'] = '******';
}

//添加或更改分类的编辑页面
shownav('global', 'nav_discuz');
showsubmenu('nav_discuz');
showtips('nav_discuz_tips');
showformheader('discuz');
showtableheader('');
showsetting('discuz_version', array('discuz[version]', array(
				array('discuz', lang('Discuz!')),
				array('discuzx', lang('Discuz!X'))
			)), $_SC['bbs_version'], 'select');
showsetting('discuz_url', 'discuz[url]', $_SC['bbs_url'], 'text');
showsetting('discuz_dbhost', 'discuz[dbhost]', $_SC['bbs_dbhost'], 'text');
showsetting('discuz_dbuser', 'discuz[dbuser]', $_SC['bbs_dbuser'], 'text');
showsetting('discuz_dbpw', 'discuz[dbpw]', $_SC['bbs_dbpw'], 'text');
showsetting('discuz_dbname', 'discuz[dbname]', $_SC['bbs_dbname'], 'text');
showsetting('discuz_dbcharset', 'discuz[dbcharset]', $_SC['bbs_dbcharset'], 'text');
showsetting('discuz_dbpre', 'discuz[dbpre]', $_SC['bbs_dbpre'], 'text');
//showsetting('discuz_key', 'discuz[key]', $_SC['bbs_key'], 'text');

showsubmit('valuesubmit');
showtablefooter();
showformfooter();
bind_ajax_form();

?>